package com.example.zhu95.mytimber.fragments;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.adapters.ItemVdragAdapter;
import com.example.zhu95.mytimber.entity.PlayQueueRecord;
import com.example.zhu95.mytimber.entity.SongBean;
import com.example.zhu95.mytimber.ui.SongItemTouchCallBack;
import com.example.zhu95.mytimber.utils.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhu95
 */
public class PlayqueueFragment extends BaseFragment {

    ItemVdragAdapter itemVdragAdapter;
    List<SongBean> songs;
    ItemTouchHelper songItemTouchHelper;
    RecyclerView recyclerView;

    public PlayqueueFragment() {
        // Required empty public constructor
    }

    public List<SongBean> getSongs() {
        return songs;
    }

    public ItemVdragAdapter getItemVdragAdapter() {
        return itemVdragAdapter;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        super.onCreateView(inflater, container, savedInstanceState);

        View view = inflater.inflate(R.layout.fragment_playqueue, container, false);
        // AppCompatActivity rootActivity = (AppCompatActivity) getActivity();

        Toolbar toolbar = view.findViewById(R.id.toolbar);
        recyclerView = view.findViewById(R.id.recycler_view_playqueue);

        rootActivity.setSupportActionBar(toolbar);
        toolbar.setTitle(R.string.playqueue);

        DrawerLayout drawer = (DrawerLayout) rootActivity.findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                rootActivity, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
        setHasOptionsMenu(true);

        //播放队列的歌曲是点击播放的歌曲所在的列表歌曲
        if (songs == null) {
            songs = new ArrayList<>();
        }
 /*
        songs = new ArrayList<>(Arrays.asList(new SongBean[SongData.songs.size()]));
        Collections.copy(songs, SongData.songs);*/

        itemVdragAdapter = new ItemVdragAdapter(songs, rootActivity);
        recyclerView.setAdapter(itemVdragAdapter);
        LinearLayoutManager manager = new LinearLayoutManager(rootActivity);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // inflater.inflate(R.menu.fragment_playlist_popup, menu);
        super.onCreateOptionsMenu(menu, inflater);
        menu.findItem(R.id.action_order_by).setVisible(false);
    }

    @Override
    public void onResume() {
        super.onResume();
        songs = PlayQueueRecord.Companion.getPlayqueueSongs();
        itemVdragAdapter.setSongs(songs);
        itemVdragAdapter.notifyDataSetChanged();

        // MARK:2017/12/20 匿名内部类实现item拖拽 reference：https://www.cnblogs.com/wjtaigwh/p/6543354.html
        songItemTouchHelper = new ItemTouchHelper(new SongItemTouchCallBack(itemVdragAdapter, songs) {
            @Override
            public void saveSequence() {
                super.saveSequence();
                String idsStrFormat = Util.INSTANCE.getIdsStrFormat(songs);
                PlayQueueRecord playQueueRecord = new PlayQueueRecord();
                playQueueRecord.setSongids(idsStrFormat);
                playQueueRecord.updateAll();
            }
        });
        // MARK:2017/12/20 附加到RecyclerView
        songItemTouchHelper.attachToRecyclerView(recyclerView);
        itemVdragAdapter.setItemTouchHelper(songItemTouchHelper);
    }

}
